package com.lw.chat.view.main;

import atlantafx.base.controls.CustomTextField;
import com.goxr3plus.fxborderlessscene.borderless.BorderlessScene;
import com.lw.chat.view.chat.ChatView;
import com.lw.chat.view.chat.ChatViewModel;
import de.saxsys.mvvmfx.FluentViewLoader;
import de.saxsys.mvvmfx.FxmlView;
import de.saxsys.mvvmfx.InjectViewModel;
import de.saxsys.mvvmfx.ViewTuple;
import io.github.palexdev.materialfx.controls.MFXButton;
import javafx.animation.FadeTransition;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Region;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.net.URL;
import java.util.ResourceBundle;

public class MainView implements FxmlView<MainViewModel>, Initializable {


    @FXML
    private BorderPane rootPane;
    @FXML
    private BorderPane centerPane;

    @FXML
    private MFXButton minimizeBut;
    @FXML
    private MFXButton maximizeBut;
    @FXML
    private MFXButton closeBut;

    @FXML
    private ToggleButton chatBut;
    @FXML
    private ToggleButton addressBookBut;


    @InjectViewModel
    private MainViewModel mainViewModel;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        rootPane.getStyleClass().add("main-view");

        Region theme = new Region();
        theme.getStyleClass().add("theme-icon");

        Region minus = new Region();
        minus.getStyleClass().add("minus-icon");

        Region resizeMax = new Region();
        resizeMax.getStyleClass().add("resize-max-icon");

        Region resizeMin = new Region();
        resizeMin.getStyleClass().add("resize-min-icon");

        Region close = new Region();
        close.getStyleClass().add("close-icon");

        mainViewModel.maximizedProperty().addListener((observableValue, oldVal, newVal) -> {
            maximizeBut.setTooltip(new Tooltip(newVal ? "还原" : "最大化"));
            maximizeBut.setGraphic(newVal ? resizeMin : resizeMax);

        });

        minimizeBut.setOnAction(actionEvent -> {

            if (minimizeBut.getScene() instanceof BorderlessScene) {
                ((BorderlessScene) maximizeBut.getScene()).minimizeStage();
            }
        });
        minimizeBut.setGraphic(minus);

        maximizeBut.setGraphic(resizeMax);
        maximizeBut.setOnAction(actionEvent -> {

            if (maximizeBut.getScene() instanceof BorderlessScene) {

                ((BorderlessScene) maximizeBut.getScene()).maximizeStage();
            }
        });

        closeBut.setGraphic(close);
        closeBut.setOnAction(actionEvent -> {
            Stage stage = (Stage) closeBut.getScene().getWindow();

            stage.close();
        });
        FadeTransition fadeTransition = new FadeTransition(Duration.millis(400), rootPane);
        fadeTransition.setFromValue(0);
        fadeTransition.setToValue(1);
        fadeTransition.play();


        chatBut.selectedProperty().addListener((observableValue, aBoolean, t1) -> {
            if (t1) {
                showChatView();
            }
        });
    }


    private void showChatView() {
        ViewTuple<ChatView, ChatViewModel> viewTuple = FluentViewLoader.fxmlView(ChatView.class).load();
        centerPane.setCenter(viewTuple.getView());

    }


}
